//
//  DJTabBarController.m
//  WeiboDemo
//
//  Created by Bworm on 15/1/28.
//  Copyright (c) 2015年 Bworm. All rights reserved.
//

#import "DJTabBarController.h"
#import "DJTabBarT.h"
#import "DJHomeViewController.h"
#import "DJMessageViewController.h"
#import "DJDiscoverViewController.h"
#import "DJProfileViewController.h"
#import "DJNavigationController.h"

@interface DJTabBarController () <DJTabBarTDelegate>

@property (nonatomic, weak) DJTabBarT *customTabBar;
@end

@implementation DJTabBarController

- (instancetype)init
{
    self = [super init];
    if (self) {
//        DDLogDebug(@"%d--%s", __LINE__, __func__);
    }
    return self;
}

// init内部会调用initWithNibName方法,所以把自定义控制器的初始化放入其中,无论调用init还是initWithNibName方法都可以初始化
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
//    DDLogDebug(@"%D--%s", __LINE__, __func__);
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        UIViewController *home = [self addControllerWithClass:[DJHomeViewController class]
                                                        title:@"首页"
                                                        image:@"tabbar_home"
                                                selectedImage:@"tabbar_home_selected"];
        UIViewController *message = [self addControllerWithClass:[DJMessageViewController class]
                                                           title:@"消息"
                                                           image:@"tabbar_message_center"
                                                   selectedImage:@"tabbar_message_center_selected"];
        UIViewController *discover = [self addControllerWithClass:[DJDiscoverViewController class]
                                                            title:@"发现"
                                                            image:@"tabbar_discover"
                                                    selectedImage:@"tabbar_discover_selected"];
        UIViewController *profile = [self addControllerWithClass:[DJProfileViewController class]
                                                           title:@"我"
                                                           image:@"tabbar_profile"
                                                   selectedImage:@"tabbar_profile_selected"];
//        self.viewControllers = @[home, message, discover, profile];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
//    // 1.创建自定义的tabBar
//    DJTabBar *tabBar = [[DJTabBar alloc] init];
//    DDLogDebug(@"替换之前的 %@", self.tabBar);
//    
//    // 2.用自定义的tabBar替换系统的
//    [self setValue:tabBar forKeyPath:@"tabBar"];
//    DDLogDebug(@"替换之后的 %@", self.tabBar);

    // 1.创建完全自定义的tabBar 设置代理
    DJTabBarT *tabBar = [[DJTabBarT alloc] init];
    tabBar.delegate = self;
    // 2.设置frame
    tabBar.frame = self.tabBar.frame;
    // 3.添加到父控件上
    [self.view addSubview:tabBar];
    self.customTabBar = tabBar;
    self.customTabBar.backgroundColor = [UIColor whiteColor];
    
    // 4.移除系统的tabBar
    [self.tabBar removeFromSuperview];
   
}

- (UIViewController *)addControllerWithClass:(Class)class title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    UIViewController *vc = [[class alloc] init];
    return [self addControllerWithVc:vc title:title image:image selectedImage:selectedImage];
}

- (UIViewController *)addControllerWithVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    vc.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:image];
//    vc.tabBarItem.image = [UIImage resizedImage:image];
    /*
     iOS7需要通过代码告诉系统不要用tabBar.tintColor来渲染选中图片
     */
    // 1.创建图片
    UIImage *newImage =  [UIImage imageNamed:selectedImage];
    // 2.告诉系统原样显示
    newImage = [newImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 3.设置图片
    vc.tabBarItem.selectedImage = newImage;
    vc.view.backgroundColor = IWRandomColor;
//    vc.view.backgroundColor = [UIColor whiteColor];
    // 设置tabBarButton的标题颜色
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor orangeColor]} forState:UIControlStateSelected];
    
    //为vc包装一个导航控制器
    DJNavigationController *nav = [[DJNavigationController alloc] initWithRootViewController:vc];
    
    // 添加控制器
    [self addChildViewController:nav];
    
    // 为每个添加的控制器创建一个自定义的按钮
    [self.customTabBar addItem:vc.tabBarItem];
    
    return vc;
}

#pragma mark - DJTabBarTDelegate
- (void)tabBar:(DJTabBarT *)tabBar selectedBtnFrom:(NSInteger)from to:(NSInteger)to
{
//    DDLogDebug(@"from = %tu  to = %tu", from, to);

    self.selectedIndex = to;
     self.tabBar.hidden = YES;
}

@end
